package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.Personalmsg;
import com.groupseven.backend.domain.TeacherAduit.TeacherAduitNow;
import com.groupseven.backend.domain.TeacherAduit.TeacherAduitPast;
import com.groupseven.backend.mapper.ProgramAduitMapper;
import com.groupseven.backend.mapper.TeacherAduitMapper;
import com.groupseven.backend.services.TeacherAduitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class TeacherAduitServiceImpl implements TeacherAduitService {
    @Autowired
    public TeacherAduitMapper teacherAduitMapper;
    @Autowired
    public ProgramAduitMapper programAduitMapper;
    public int getnowPaperGrade(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.EndTime=new Date();
        teacherAduitNow.username=username;
        int basecount=0;
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowPapercount(teacherAduitNow)!=null){
            basecount=teacherAduitMapper.getnowPapercount(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowPaperBaseGrade()!=null){
            basegrade=teacherAduitMapper.getnowPaperBaseGrade();
        }
        if(teacherAduitMapper.getnowPaperjiachengGrade(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowPaperjiachengGrade(teacherAduitNow);
        }
        return basecount*basegrade+jiachenggrade;
    }

    public int getnowPatentGrade(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basecount=0;
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowPatentcount(teacherAduitNow)!=null){
            basecount=teacherAduitMapper.getnowPatentcount(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowPatentBaseGrade()!=null){
            basegrade=teacherAduitMapper.getnowPatentBaseGrade();
        }
        if(teacherAduitMapper.getnowPatentjiachengGrade(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowPatentjiachengGrade(teacherAduitNow);
        }
        return basecount*basegrade+jiachenggrade;
    }

    public int getnowProgramGrade(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basecount=0;
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowProgramcount(teacherAduitNow)!=null){
            basecount=teacherAduitMapper.getnowProgramcount(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowProgramBaseGrade()!=null){
            basegrade=teacherAduitMapper.getnowProgramBaseGrade();
        }
        if(teacherAduitMapper.getnowProgramjiachengGrade(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowProgramjiachengGrade(teacherAduitNow);
        }
        return basecount*basegrade+jiachenggrade;
    }

    public int getnowPrizeProductGrade(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basecount=0;
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowPrizeProductcount(teacherAduitNow)!=null){
            basecount=teacherAduitMapper.getnowPrizeProductcount(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowPrizeProductBaseGrade()!=null){
            basegrade=teacherAduitMapper.getnowPrizeProductBaseGrade();
        }
        if(teacherAduitMapper.getnowPrizeProductjiachengGrade(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowPrizeProductjiachengGrade(teacherAduitNow);
        }
        return basecount*basegrade+jiachenggrade;
    }

    ////////
    public int addTeacherAduitNow(TeacherAduitNow teacherAduitNow){
        return teacherAduitMapper.addTeacherAduitNow(teacherAduitNow);
    }

    public int updateTeacherAduitNow(TeacherAduitNow teacherAduitNow){
        return teacherAduitMapper.updateTeacherAduitNow(teacherAduitNow);
    }

    public int deleteTeacherAduitNow(String username){
        return teacherAduitMapper.deleteTeacherAduitNow(username);
    }

    public int addTeacherAduitPast(TeacherAduitPast teacherAduitPast){
        return teacherAduitMapper.addTeacherAduitPast(teacherAduitPast);
    }

    public int updateTeacherAduitPast(TeacherAduitPast teacherAduitPast){
        return teacherAduitMapper.updateTeacherAduitPast(teacherAduitPast);
    }

    public int deleteTeacherAduitPast(String username){
        return teacherAduitMapper.deleteTeacherAduitPast(username);
    }

    public Personalmsg selectPersonalmsgByusername(String username){
        return teacherAduitMapper.selectPersonalmsgByusername(username);
    }

    public int selectAcessGradeByTeacherType(String TeacherType){
        return teacherAduitMapper.selectAcessGradeByTeacherType(TeacherType);
    }

    public TeacherAduitNow selectTeacherAduitNowByusername(String username){
        return teacherAduitMapper.selectTeacherAduitNowByusername(username);
    }
    ////////////plan////////////////////////
    public int getnowPaperGradeByplan(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.Belonging=(teacherAduitMapper.getBelongingByusername(username));
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowPaperBaseGradeByplan(teacherAduitNow)!=null){
            basegrade=teacherAduitMapper.getnowPaperBaseGradeByplan(teacherAduitNow);

        }
        if(teacherAduitMapper.getnowPaperjiachengGradeByplan(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowPaperjiachengGradeByplan(teacherAduitNow);

        }
        return basegrade+jiachenggrade;
    }

    public int getnowPatentGradeByplan(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.Belonging=(teacherAduitMapper.getBelongingByusername(username));
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowPatentBaseGradeByplan(teacherAduitNow)!=null){
            basegrade=teacherAduitMapper.getnowPatentBaseGradeByplan(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowPatentjiachengGradeByplan(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowPatentjiachengGradeByplan(teacherAduitNow);
        }
        return basegrade+jiachenggrade;
    }

    public int getnowProgramGradeByplan(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.Belonging=(teacherAduitMapper.getBelongingByusername(username));
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowProgramBaseGradeByplan(teacherAduitNow)!=null){
            basegrade=teacherAduitMapper.getnowProgramBaseGradeByplan(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowProgramjiachengGradeByplan(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowProgramjiachengGradeByplan(teacherAduitNow);
        }
        return basegrade+jiachenggrade;
    }

    public int getnowPrizeProductGradeByplan(String username){
        TeacherAduitNow teacherAduitNow= teacherAduitMapper.getStartTime(username);
        teacherAduitNow.Belonging=(teacherAduitMapper.getBelongingByusername(username));
        teacherAduitNow.EndTime=(new Date());
        teacherAduitNow.username=(username);
        int basegrade=0;
        int jiachenggrade=0;
        if(teacherAduitMapper.getnowPrizeProductBaseGradeByplan(teacherAduitNow)!=null){
            basegrade=teacherAduitMapper.getnowPrizeProductBaseGradeByplan(teacherAduitNow);
        }
        if(teacherAduitMapper.getnowPrizeProductjiachengGradeByplan(teacherAduitNow)!=null){
            jiachenggrade=teacherAduitMapper.getnowPrizeProductjiachengGradeByplan(teacherAduitNow);
        }
        return basegrade+jiachenggrade;
    }
    ///////////////plan//////////////////////

    public List<TeacherAduitNow> getallTeacherAduitNow(){
        return teacherAduitMapper.getallTeacherAduitNow();
    }

    //////////////////定时器检测////////////////////"*/15 * * * * ?""0 0 23 * * ?"
    @Scheduled(cron = "0 0 23 * * ?")
    public void updateAduitTurn(){
        List<TeacherAduitNow> teacherAduitNows=teacherAduitMapper.getallTeacherAduitNow();
        Date date=new Date();
        for(TeacherAduitNow teacherAduitNow:teacherAduitNows){

            if (date.compareTo(teacherAduitMapper.getEndTimeByusername(teacherAduitNow.username))== 1){
                teacherAduitMapper.addTeacherAduitPast(fuzhi(teacherAduitNow));

                teacherAduitNow.StartTime=teacherAduitNow.EndTime;
                Calendar calendar=Calendar.getInstance();
                calendar.setTime(teacherAduitNow.StartTime);
                calendar.add(Calendar.YEAR,teacherAduitNow.AduitYears);
                teacherAduitNow.EndTime=calendar.getTime();
                teacherAduitNow.TotalGrade=0;
                teacherAduitNow.ProgramGrade=0;
                teacherAduitNow.PatentGrade=0;
                teacherAduitNow.PrizeProductGrade=0;
                teacherAduitNow.PaperGrade=0;

                teacherAduitMapper.updateTeacherAduitNow(teacherAduitNow);
            }
        }

    }

    public TeacherAduitPast fuzhi(TeacherAduitNow teacherAduitNow){
        TeacherAduitPast teacherAduitPast=new TeacherAduitPast();
        teacherAduitPast.AduitYears=teacherAduitNow.AduitYears;
        teacherAduitPast.TeacherType=teacherAduitNow.TeacherType;
        teacherAduitPast.AcessGrade=teacherAduitNow.AcessGrade;
        teacherAduitPast.EndTime=teacherAduitNow.EndTime;
        teacherAduitPast.StartTime=teacherAduitNow.StartTime;
        teacherAduitPast.EmployeeNumber=teacherAduitNow.EmployeeNumber;
        teacherAduitPast.PaperGrade=teacherAduitNow.PaperGrade;
        teacherAduitPast.PatentGrade=teacherAduitNow.PatentGrade;
        teacherAduitPast.PrizeProductGrade=teacherAduitNow.PrizeProductGrade;
        teacherAduitPast.ProgramGrade=teacherAduitNow.ProgramGrade;
        teacherAduitPast.TotalGrade=teacherAduitNow.TotalGrade;
        teacherAduitPast.RealName=teacherAduitNow.RealName;
        teacherAduitPast.username=teacherAduitNow.username;
        if(teacherAduitNow.TotalGrade<teacherAduitNow.AcessGrade){
            teacherAduitPast.isQualified="不合格";
        }
        else teacherAduitPast.isQualified="合格";
        return teacherAduitPast;
    }


}
